package cn.edu.scau.cmi.houzhicong.hibernate.gui;

import cn.edu.scau.cmi.houzhicong.combinationPattern.safetyPattern.SafetyComponent;
import cn.edu.scau.cmi.houzhicong.combinationPattern.safetyPattern.SafetyOrganization;
import cn.edu.scau.cmi.houzhicong.combinationPattern.safetyPattern.SafetyPerson;
import cn.edu.scau.cmi.houzhicong.hibernate.dao.StudentDao;
import cn.edu.scau.cmi.houzhicong.hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.houzhicong.hibernate.domain.Student;
import cn.edu.scau.cmi.houzhicong.hibernate.domain.Teacher;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

public class HibernateController implements Initializable {

    Map<String, SafetyComponent> componentMap = new HashMap<>();





    @FXML
    private TextField idText;

    @FXML
    private TextField nameText;

    @FXML
    private Button addStudentButton;
    @FXML
    private Button deleteStudentButton;
    @FXML
    private Button updateStudentButto;

    @FXML
    private Button addTeacherButton;
    @FXML
    private Button deleteTeacherButton;
    @FXML
    private Button updateTeacherButton;

    @FXML
    private TextField teacherText;
    @FXML
    private TextField studentText;
    @FXML
    private Button addTutorButton;
    @FXML
    private Button deleteTutorButton;
    @FXML
    private Button showTutorButton;
    @FXML
    private Button showStudentsButton;

    @FXML
    private TextField infoArea;


    private StudentDao studentDao = new StudentDao();
    private TeacherDao teacherDao = new TeacherDao();

    @Override
    public void initialize(URL location, ResourceBundle resources) {}


    public void addStudent(){
        String id = idText.getText();
        String name = nameText.getText();
        boolean finished = studentDao.save(id,name);
        if(finished){
            infoArea.setText(String.format("添加学生{id:%s,name:%s}成功",id,name));
        }
    }


    public void deleteStudent(){
        String id = idText.getText();
        String name = nameText.getText();
        if(id.equals("")){
            infoArea.setText(String.format("请输入id"));
            return;
        }
        Student student = studentDao.findById(id);
        if(student==null){
            infoArea.setText(String.format("该学生不存在"));
        }
        studentDao.deleteById(id);
        infoArea.setText(String.format("删除成功"));

    }


    public void updateStudent(){
        String id = idText.getText();
        String name = nameText.getText();
        if(id.equals("")){
            infoArea.setText(String.format("请输入id"));
            return;
        }
        Student student = studentDao.findById(id);
        if(student==null){
            infoArea.setText(String.format("该学生不存在"));
        }else{
            studentDao.update(new Student(id,name));
            infoArea.setText(String.format("修改成功"));
        }
    }


    public void addTeacher(){
        String id = idText.getText();
        String name = nameText.getText();
        boolean finished = teacherDao.save(id,name);
        if(finished){
            infoArea.setText(String.format("添加老师{id:%s,name:%s}成功",id,name));
        }
    }

    public void deleteTeacher(){
        String id = idText.getText();
        String name = nameText.getText();
        if(id.equals("")){
            infoArea.setText(String.format("请输入id"));
            return;
        }
        Teacher teacher = teacherDao.findById(id);
        if(teacher==null){
            infoArea.setText(String.format("该老师不存在"));
        }
        studentDao.deleteById(id);
        infoArea.setText(String.format("删除成功"));
    }


    public void updateTeacher(){
        String id = idText.getText();
        String name = nameText.getText();
        if(id.equals("")){
            infoArea.setText(String.format("请输入id"));
            return;
        }
        Teacher teacher = teacherDao.findById(id);
        if(teacher==null){
            infoArea.setText(String.format("该老师不存在"));
        }else{
            studentDao.update(new Student(id,name));
            infoArea.setText(String.format("修改成功"));
        }
    }


    public void addTutor(){
        String teacherId = teacherText.getText();
        String studentId = studentText.getText();
        if(teacherId.equals("")){
            infoArea.setText(String.format("请输入老师id"));
        }else if(studentId.equals("")){
            infoArea.setText(String.format("请输入学生id"));
        }else{
            Teacher teacher = teacherDao.findById(teacherId);
            Student student = studentDao.findById(studentId);
            if(teacher==null){
                infoArea.setText(String.format("该老师不存在"));
            }else if(student==null){
                infoArea.setText(String.format("该学生不存在"));
            }else{
                teacherDao.addStudent(teacher,student);
                infoArea.setText(String.format("设置指导关系成功"));
            }
        }



    }

    public void deleteTutor(){
        String teacherId = teacherText.getText();
        String studentId = studentText.getText();
        if(teacherId.equals("")){
            infoArea.setText(String.format("请输入老师id"));
        }else if(studentId.equals("")){
            infoArea.setText(String.format("请输入学生id"));
        }else{
            Teacher teacher = teacherDao.findById(teacherId);
            Student student = studentDao.findById(studentId);
            if(teacher==null){
                infoArea.setText(String.format("该老师不存在"));
            }else if(student==null){
                infoArea.setText(String.format("该学生不存在"));
            }
//            else if(!teacher.getStudents().c){
//                infoArea.setText(String.format("该老师和学生不存在指导关系"));
//            }
            else{
                teacherDao.deleteStudent(teacher,student);
                infoArea.setText(String.format("解除指导关系成功"));
            }
        }

    }


    public void showTutor(){
        String studentId = studentText.getText();
       if(studentId.equals("")){
            infoArea.setText(String.format("请输入学生id"));
        }else{

           Student student = studentDao.findById(studentId);
           if(student.getTutor()==null){
               infoArea.setText(String.format("该学生还没有指导老师"));
           }else{
               Teacher teacher = teacherDao.findById(student.getTutor().getId());
               infoArea.setText(String.format("该学生的指导老师为:{id:%s,name:%s}",teacher.getId(),teacher.getName()));
           }
         }

    }


    public void showStudents(){
        String teacherId = teacherText.getText();
        if(teacherId.equals("")){
            infoArea.setText(String.format("请输入老师id"));
        }else{

            Teacher teacher = teacherDao.findById(teacherId);
            if(teacher==null){
                infoArea.setText(String.format("该老师不存在"));
            }
            else{
                StringBuilder sb  = new StringBuilder();
                for (Student student :
                        teacher.getStudents()) {
                    sb.append(String.format("{id:%s,name:%s}\n", student.getId(), student.getName()));
                }
                infoArea.setText(sb.toString());
            }
        }
    }

}
